/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Config;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.UserService;
import etcore.webapp.grouptalk.util.DateUtil;
import etcore.webapp.grouptalk.util.MD5;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * This action validates the request and executes the command of login, then set
 * the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class LoginAction extends AbstractAction {
 
	private static final long serialVersionUID = 318454407999282416L;
	// validate result
	public static final int LOGIN_START = 0;
	public static final int LOGIN_SUCCESS = 1;
	public static final int LOGIN_FAILURE = 2;

	// parameter constants

	public static final String CMD_VALUE = "userLogin";
	public static final String USER_NAME = "userName";
	public static final String USER_PWD = "userPwd";

	// utility constants
	public static final String REF_STR = "ref";
	public static final String REFERER_STR = "referer";
	public static final String URL_HOME_STR = "groupdata.";
	public static final String URL_LOGIN_STR = "login.";
	public static final String URL_LOGOUT_STR = "logout.";
	public static final String NULL_STR = "null";
	public static final String LOGIN = "login";

	// forward pages
	public static final String START_PAGE = "/group/login.jsp";
	public static final String FAILURE_PAGE = "/error.jsp";
	public static final String USER_CONTROL_PAGE = "/group/myInfo.action";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_VALUE.equals(cmd)) {
			String userName = request.getParameter(USER_NAME);
			String userPwd = request.getParameter(USER_PWD);
			UserService userService = new UserService();
			MD5 md5 = new MD5();
			long result = userService
					.verify(userName, md5.getMD5ofStr(userPwd));

			if (1 == result) {
				return LOGIN_SUCCESS;

			} else {
				return LOGIN_FAILURE;
			}
		} else {
			return LOGIN_START;
		}

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		return null;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub
		if (LOGIN_START == validationResult)
			return START_PAGE;
		else if (LOGIN_SUCCESS == validationResult) {
			String ref = request.getParameter(REF_STR);
			// System.out.println(ref);
			if (ref != null && !ref.equals(Symbols.BLANK)
					&& !ref.equals(NULL_STR)
					&& ref.indexOf(URL_LOGIN_STR) == -1) {

				return ref;
			} else if (request.getHeader(REFERER_STR) != null
					&& request.getHeader(REFERER_STR).indexOf(URL_LOGIN_STR) == -1
					&& request.getHeader(REFERER_STR).indexOf(URL_HOME_STR) == -1
					&& request.getHeader(REFERER_STR).indexOf(URL_LOGOUT_STR) == -1) {
				{

					return request.getHeader(REFERER_STR);
				}
			} else
				return USER_CONTROL_PAGE;

		} else
			return FAILURE_PAGE;

	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {

		HttpSession session = request.getSession(true);
		if (LOGIN_SUCCESS == validationResult) {

			String userName = request.getParameter(USER_NAME);
			UserService userService = new UserService();
			session.setAttribute(LOGIN, Messages.TRUE);
			User sessionUser = userService.findByUserName(userName);
			sessionUser.setIsOnline(new Integer(1));
			sessionUser.setLoginNum(new Integer(sessionUser.getLoginNum()
					.intValue() + 1));
			sessionUser.setLastIp(request.getRemoteAddr());//
			sessionUser.setLastTime(DateUtil.getTime(Config.getProperty(Config.HOURS_DIFFERENCE)));//
			userService.merge(sessionUser);
			session.setAttribute(Messages.SESSION_USER, sessionUser);
		} else if (LOGIN_FAILURE == validationResult) {
			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (lang != null && lang.equals(Messages.LANG_EN)) {
				msg = Messages.getELabel(Messages.MSG_LOGIN_ERROR);
			} else {
				msg = Messages.getLabel(Messages.MSG_LOGIN_ERROR);
			}
			request.setAttribute(Messages.MSG, msg);
		}

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
